<script>
import '@assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import QcReportModal from './modules/QcReportModal.vue'
import { postAction } from '@api/manage'
import store from '@/store'

export default {
  // 原料检验报告单
  name: 'RawMaterialInspectionList',
  components: { QcReportModal },
  mixins: [JeecgListMixin, mixinDevice],
  props: {
    status: {
      type: String,
      default: '0'
    }
  },
  data() {
    return {
      // 表头
      defaultColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          fixed: 'left',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '入库类型',
          align: 'center',
          dataIndex: 'entryType_dictText',
          width: 80,
          fixed: 'left'
        },
        /*
        {
          title: '入库单号',
          align: 'center',
          dataIndex: 'entryNumber',
          width: 80,
          fixed: 'left'
        },
         */
        {
          title: '检验单号',
          align: 'center',
          dataIndex: 'reportNumber',
          width: 80,
          fixed: 'left'
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'quantity'
        },
        {
          title: '批次号',
          align: 'center',
          dataIndex: 'batchNumber'
        },
        {
          title: '物料批号',
          align: 'center',
          dataIndex: 'materialBatchNumber'
        },
        {
          title: '储位',
          align: 'center',
          dataIndex: 'storageLocation_dictText'
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'entryDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '生产日期',
          align: 'center',
          dataIndex: 'productionDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '保质期（天）',
          align: 'center',
          dataIndex: 'shelfLife'
        },
        {
          title: '限用日期',
          align: 'center',
          dataIndex: 'expirationDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'creator_dictText'
        },
        {
          title: '制单人部门',
          align: 'center',
          dataIndex: 'creatorDeptCode_dictText'
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'creationDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '制单人备注',
          align: 'center',
          dataIndex: 'creatorRemark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/qms/inspection/report/raw-material/list',
        delete: '/qms/inspection/report/delete',
        deleteBatch: '/qms/inspection/report/deleteBatch',
        exportXlsUrl: '/qms/inspection/report/raw-material/exportXls'
      },
      dictOptions: {},
      superFieldList: [],
      settingColumns: [
        'entryType_dictText',
        'reportNumber',
        'materialCode',
        'materialName',
        'specificationModel',
        'unit_dictText',
        'quantity',
        'batchNumber',
        'materialBatchNumber',
        'storageLocation_dictText',
        'entryDate',
        'productionDate',
        'shelfLife',
        'expirationDate',
        'creator_dictText',
        'creatorDeptCode_dictText',
        'creationDate',
        'creatorRemark'
      ],
      disableMixinCreated: true
    }
  },
  created() {
    if (this.status) {
      this.queryParam.status = this.status
    }
    this.settingColumns = this.settingColumns;
    this.loadData()
  },
  computed: {
    columns: {
      get(){
         // 根据status和settingColumns动态生成列
         return this.defaultColumns.filter(item => {
          // 始终显示行号列
          if (item.key === 'rowIndex') return true
          // 根据状态显示操作列
          if (item.dataIndex === 'action') return this.status === '1'
          // 其他列根据settingColumns决定是否显示
          return this.settingColumns.includes(item.dataIndex)
        })
      },
      set() {
      }
    }
  },
  methods: {
    handleAdd: function() {
      if (!this.selectionRows || this.selectionRows.length === 0) {
        this.$message.warning('请选择报告单')
        return
      }
      const firstMaterialCode = this.selectionRows[0].materialCode
      if (this.selectionRows.some(row => row.materialCode !== firstMaterialCode)) {
        this.$message.warning('请选择相同物料的报告单')
        return
      }
      this.$refs.modalForm.edit(this.selectionRows)
      this.$refs.modalForm.title = '添加'
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.reportType = 'raw-material'
    },
    handleDetail: function(record) {
      this.$refs.modalForm.detail(record)
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.disableSubmit = true
      this.$refs.modalForm.reportType = 'raw-material'
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    handleCancel(record) {
      postAction('/qms/inspection/report/revoke', { id: record.id })
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
          } else {
            this.$message.warning(res.message)
          }
        })
    },
    printPreview(record) {
      let token = store.getters.token
      let id = record.id
      window.open(`${window._CONFIG['domianURL']}/jmreport/view/1098781098909704192?id=${id}`, '_blank')
    },
    initDictConfig() {
    }
  }
}
</script>

<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-if="status === '0'">添加</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('原料检验报告单')">导出</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <span style="float:right;">
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row style="width: 400px">
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!=='rowIndex'&& item.dataIndex!=='action'">
                        <a-col :span="12"><a-checkbox :value="item.dataIndex"><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />设置</a >
          </a-popover>
        </span>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type:'checkbox'}"
        class="j-table-force-nowrap1"
        @change="handleTableChange">
        <span slot="action" slot-scope="text, record">
          <template v-if="status === '1'">
            <a @click="handleDetail(record)">详情</a>
            <a-divider type="vertical" />
            <a-popconfirm
              title="确定撤销吗?"
              @confirm="() => handleCancel(record)">
              <a>撤销</a>
            </a-popconfirm>
            <a-divider type="vertical" />
            <a @click="printPreview(record)">单据打印</a>
          </template>
        </span>
      </a-table>
    </div>
    <qc-report-modal ref="modalForm" @ok="modalFormOk" />
  </a-card>
</template>

<style scoped>
@import '~@assets/less/common.less';
</style>